package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.dto.VisitDto;
import com.zzyl.entity.Visit;
import com.zzyl.service.CustomerUserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description CustomerUserController
 * @Author luohai
 * @Date 2024-10-20
 */
@Slf4j
@Api(tags = "客户管理")
@RestController
@RequestMapping("/customer")
public class CustomerUserController extends BaseController {

    @Autowired
    private CustomerUserService customerUserService;

    /**
     * C端用户登录--微信登录
     *
     * @param userLoginRequestDto 用户登录信息
     * @return 登录结果
     */
    @PostMapping("/user/login")
    @ApiOperation("登录")
    public ResponseResult<LoginVo> login(@RequestBody UserLoginRequestDto userLoginRequestDto) {
        LoginVo loginVo = customerUserService.login(userLoginRequestDto);
        return success(loginVo);
    }

    /**
     * 用户端护理项目分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/orders/project/page")
    @ApiOperation(value = "小程序端护理项目分页查询")
    @ApiOperationSupport(includeParameters = {"name", "pageNum", "pageSize", "status"})
    public ResponseResult<PageResponse<NursingProjectVo>> userNursingPaginationQuery(@ApiParam(value = "护理项目名称") String name, @ApiParam(value = "当前页数", defaultValue = "1") Integer pageNum, @ApiParam(value = "当页显示数", defaultValue = "5") Integer pageSize, @ApiParam(value = "状态") Integer status) {
        PageResponse<NursingProjectVo> pageResponse=customerUserService.userNursingPaginationQuery(name,pageNum,pageSize,status);
        return success(pageResponse);
    }

    /**
     * 小程序端根据id查询护理项目
     * @param id
     * @return
     */
    @GetMapping("/orders/project/{id}")
    @ApiOperation(value = "小程序端根据id查询护理项目")
    public ResponseResult<NursingProjectVo> searchUserNursingProjectById(@ApiParam("护理项目id") @PathVariable Integer id){
        NursingProjectVo nursingProjectVo=customerUserService.searchUserNursingProjectById(id);
        return success(nursingProjectVo);
    }

    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/reservation/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult queryTheNumberOfCancelledAppointments(){
        Long id = UserThreadLocal.getUserId();
        Integer count=customerUserService.queryTheNumberOfCancelledAppointments(id);
        return success(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @GetMapping("/reservation/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult<List<TimeCountVo>> remainingReservationTimes(@ApiParam("时间戳") Long time){
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
        List<TimeCountVo> timeCountVo=customerUserService.remainingReservationTimes(localDateTime);
        return success(timeCountVo);
    }
    /**
     * 新增预约
     * @param visit
     * @return
     */
    @PostMapping("/reservation")
    @ApiOperation("新增预约")
    public ResponseResult addAppointment(@ApiParam("visitDto预约对象") @RequestBody Visit visit){
        customerUserService.addAppointment(visit);
        return success();
    }

    /**
     * 分页查询预约
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/reservation/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<VisitVo>> pageQueryReservation(@ApiParam("页码") Integer pageNum, @ApiParam("每页数量") Integer pageSize, @ApiParam("状态") Integer status){
        PageResponse<VisitVo> pageResponse=customerUserService.pageQueryReservation(pageNum,pageSize,status);
        return success(pageResponse);
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @PutMapping("/reservation/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult cancelReservation(@PathVariable Long id){
        customerUserService.cancelReservation(id);
        return success();
    }
}

